package dao;

import model.Farm;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class AddFarmDatebase {
    private String dbdriver="com.mysql.jdbc.Driver";
    private String dburl="jdbc:mysql://localhost:3306/sctu-javaweb-2018?serverTimezone=UTC&characterEncoding=UTF-8";
    private String dbuser="root";
    private String dbpassword="123456";
    public void addFarm(Farm farm)throws Exception{
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        Class.forName(dbdriver);
        connection= DriverManager.getConnection(dburl,dbuser,dbpassword);

        String sql="INSERT INTO farm(username,farmname,area,price,breed,already_breed,farm_img)" +
                "VALUES(?,?,?,?,?,?,?)";
        preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,farm.getUsername());
        preparedStatement.setString(2,farm.getFarmName());
        preparedStatement.setString(3,farm.getArea());
        preparedStatement.setFloat(4,farm.getPrice());
        preparedStatement.setString(5,farm.getBreed());
        preparedStatement.setString(6,farm.getAlreadBreed());
        preparedStatement.setString(7,farm.getFarmImg());
        preparedStatement.execute();
        connection.close();
        preparedStatement.close();
    }
}
